﻿using Dapper;
using System;
using BCEntity.TMS.VehicleBusinessMapping;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;

namespace BCData.TMS.VehicleBusinessMapping
{
	/// <summary>
	/// 车辆与业务配置
	/// </summary>
	public class VehicleBusinessMappingData : IVehicleBusinessMappingData
	{
		private readonly IDatabaseContext databaseContext;
		private const string QUERY_EXISTS = @"SELECT 1 FROM vehiclebusinessmapping";
		private const string QUERY = @"SELECT VehicleBusinessMappingId,UpdateTime,EditAdminId,EditAdminName,VehicleTransportBusinessConfigId,VehicleTypeName,
											TransportBusiness,VehicleTypeId,Enabled,VehicleTypeRemark,MaxWithCarPeople,CreateAdminId,CreateAdminName,CreateTime,
											VehicleCategoryReferenceId,VehicleCategory,VehicleCategoryName
										FROM vehiclebusinessmapping";
		public VehicleBusinessMappingData(IDatabaseContext databaseContext)
		{
			this.databaseContext = databaseContext;
		}
		#region 检查车辆与业务配置是否存在
		public async Task<bool> Exists(int vehicleBusinessMappingId)
		{
			string sql = string.Format(@"{0} WHERE VehicleBusinessMappingId=?VehicleBusinessMappingId", QUERY_EXISTS);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?VehicleBusinessMappingId", vehicleBusinessMappingId, DbType.Int32);
			return await this.databaseContext.Master.ExecuteScalarAsync(sql, parameters) != null;
		}
		#endregion

		#region 添加车辆与业务配置
		public async Task<VehicleBusinessMappingEntity> Add(VehicleBusinessMappingEntity entity)
		{
			string sql = SqlCommands.GetInsertSql("vehiclebusinessmapping", new string[] { "VehicleTransportBusinessConfigId", "TransportBusiness", "VehicleTypeId","VehicleTypeRemark",
				 "CreateAdminId", "CreateAdminName", "MaxWithCarPeople","VehicleTypeName","VehicleCategoryReferenceId", "VehicleCategory","VehicleCategoryName"});
			sql = string.Format("{0} {1} WHERE VehicleBusinessMappingId=LAST_INSERT_ID();", sql, QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?VehicleTransportBusinessConfigId", entity.VehicleTransportBusinessConfigId, DbType.Int32, size: 11);
			parameters.Add("?TransportBusiness", entity.TransportBusiness.ToString(), DbType.AnsiString, size: 20);
			parameters.Add("?VehicleTypeId", entity.VehicleTypeId, DbType.Int32, size: 11);
			parameters.Add("?VehicleTypeRemark", entity.VehicleTypeRemark?.Trim(), DbType.String);
			parameters.Add("?CreateAdminId", entity.CreateAdminId, DbType.Int64, size: 20);
			parameters.Add("?CreateAdminName", entity.CreateAdminName?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?MaxWithCarPeople", entity.MaxWithCarPeople, DbType.Int32, size: 11);
			parameters.Add("?VehicleTypeName", entity.VehicleTypeName?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?VehicleCategoryReferenceId", entity.VehicleCategoryReferenceId, DbType.Int64);
			parameters.Add("?VehicleCategory", entity.VehicleCategory, DbType.AnsiString);
			parameters.Add("?VehicleCategoryName", entity.VehicleCategoryName, DbType.AnsiString);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<VehicleBusinessMappingEntity>(sql, parameters, databaseContext.Transaction);

		}
		#endregion

		#region 修改车辆与业务配置
		public async Task<VehicleBusinessMappingEntity> Update(VehicleBusinessMappingEntity entity)
		{
			string sql = SqlCommands.GetUpdateSql("vehiclebusinessmapping", new string[] {  "EditAdminId", "EditAdminName", "VehicleTransportBusinessConfigId", 
				"TransportBusiness", "VehicleTypeId","VehicleTypeRemark", "MaxWithCarPeople","VehicleTypeName","VehicleCategoryReferenceId", "VehicleCategory","VehicleCategoryName" }, new string[] { "VehicleBusinessMappingId" });
			sql = string.Format("{0} {1} WHERE VehicleBusinessMappingId=?VehicleBusinessMappingId;", sql, QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?VehicleBusinessMappingId", entity.VehicleBusinessMappingId, DbType.Int32, size: 11);
			parameters.Add("?EditAdminId", entity.EditAdminId, DbType.Int64, size: 20);
			parameters.Add("?EditAdminName", entity.EditAdminName?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?VehicleTransportBusinessConfigId", entity.VehicleTransportBusinessConfigId, DbType.Int32, size: 11);
			parameters.Add("?TransportBusiness", entity.TransportBusiness.ToString(), DbType.AnsiString, size: 20);
			parameters.Add("?VehicleTypeId", entity.VehicleTypeId, DbType.Int32, size: 11);
			parameters.Add("?VehicleTypeRemark", entity.VehicleTypeRemark?.Trim(), DbType.String);
			parameters.Add("?MaxWithCarPeople", entity.MaxWithCarPeople, DbType.Int32, size: 11);
			parameters.Add("?VehicleTypeName", entity.VehicleTypeName?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?VehicleCategoryReferenceId", entity.VehicleCategoryReferenceId, DbType.Int64);
			parameters.Add("?VehicleCategory", entity.VehicleCategory, DbType.AnsiString);
			parameters.Add("?VehicleCategoryName", entity.VehicleCategoryName, DbType.AnsiString);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<VehicleBusinessMappingEntity>(sql, parameters, databaseContext.Transaction);
		}
		#endregion

		#region 删除车辆与业务配置
		public async Task<bool> Delete(int vehicleBusinessMappingId)
		{
			string sql = @"DELETE FROM vehiclebusinessmapping WHERE VehicleBusinessMappingId=?VehicleBusinessMappingId";
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?VehicleBusinessMappingId", vehicleBusinessMappingId, DbType.Int32);
			return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
		}
		#endregion

		#region 获取车辆与业务配置
		public async Task<VehicleBusinessMappingEntity> GetEntity(int vehicleBusinessMappingId)
		{
			string sql = string.Format(@"{0} WHERE Enabled=1 AND VehicleBusinessMappingId=?VehicleBusinessMappingId", QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?VehicleBusinessMappingId", vehicleBusinessMappingId, DbType.Int64);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<VehicleBusinessMappingEntity>(sql, parameters);
		}
		#endregion

		#region 获取车辆与业务配置列表
		public async Task<IEnumerable<VehicleBusinessMappingEntity>> GetList()
		{
			string sql = string.Format(@"{0} WHERE Enabled=1", QUERY);
			return await this.databaseContext.Master.QueryAsync<VehicleBusinessMappingEntity>(sql);
		}
		#endregion

		#region 获取车辆与业务配置分页
		public async Task<(IEnumerable<VehicleBusinessMappingEntity>, int)> Query(int? vehicleTransportBusinessConfigId, int? vehicleTypeId, bool? enabled, DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
		{
			List<string> wheres = new List<string>();
			DynamicParameters parameters = new DynamicParameters();
            if (vehicleTransportBusinessConfigId.HasValue)
            {
				wheres.Add("VehicleTransportBusinessConfigId=?VehicleTransportBusinessConfigId");
				parameters.Add("?VehicleTransportBusinessConfigId", vehicleTransportBusinessConfigId.Value, DbType.Int32);
			}
            if (vehicleTypeId.HasValue)
            {
				wheres.Add("VehicleTypeId=?VehcileTypeId");
				parameters.Add("?VehicleTypeId", vehicleTypeId.Value, DbType.Int32);
            }
			if (enabled.HasValue)
			{
				wheres.Add("Enabled=?Enabled");
				parameters.Add("?Enabled", enabled.Value, DbType.Boolean);
			}
			if (start.HasValue && !end.HasValue)
			{
				wheres.Add("CreateTime>=?Start");
				parameters.Add("?Start", start.Value, DbType.DateTime);
			}
			else if (!start.HasValue && end.HasValue)
			{
				wheres.Add("CreateTime<=?End");
				parameters.Add("?End", end.Value, DbType.DateTime);
			}
			else if (start.HasValue && end.HasValue)
			{
				wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
				parameters.Add("?Start", start.Value, DbType.DateTime);
				parameters.Add("?End", end.Value, DbType.DateTime);
			}
			string sql = string.Format(@"{0} {1} ORDER BY {4} {5} LIMIT {2},{3}; SELECT COUNT(1) FROM vehiclebusinessmapping {1}",
				  QUERY,
				  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
				  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
				  pageSize,
				  string.IsNullOrWhiteSpace(sortName) ? "CreateTime" : sortName,
				  ascending.HasValue && ascending.Value ? "ASC" : "DESC");
			return await this.databaseContext.QueryMultipleAsync<VehicleBusinessMappingEntity, int>(sql, parameters);
		}
		#endregion

		#region 启用车辆与业务配置
		public async Task<bool> Enabled(int vehicleBusinessMappingId, long adminId, string fullname)
		{
			string sql = @"UPDATE vehiclebusinessmapping SET Enabled=1,EditAdminId=?EditAdminId,EditAdminName=?EditAdminName  WHERE VehicleBusinessMappingId=?VehicleBusinessMappingId";
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?VehicleBusinessMappingId", vehicleBusinessMappingId, DbType.Int32);
			parameters.Add("?EditAdminId", adminId, DbType.Int64, size: 20);
			parameters.Add("?EditAdminName", fullname, DbType.AnsiString, size: 20);
			return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
		}
		#endregion

		#region 禁用车辆与业务配置
		public async Task<bool> Disabled(int vehicleBusinessMappingId, long adminId, string fullname)
		{
			string sql = @"UPDATE vehiclebusinessmapping SET Enabled=0,EditAdminId=?EditAdminId,EditAdminName=?EditAdminName  WHERE VehicleBusinessMappingId=?VehicleBusinessMappingId";
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?VehicleBusinessMappingId", vehicleBusinessMappingId, DbType.Int32);
			parameters.Add("?EditAdminId", adminId, DbType.Int64, size: 20);
			parameters.Add("?EditAdminName", fullname, DbType.AnsiString, size: 20);
			return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
		}
		#endregion

		#region 获取司机端所用车辆列表
		public async Task<IEnumerable<VehicleBusinessMappingForAppEntity>> GetListForApp()
		{
			string sql = @"SELECT A.VehicleBusinessMappingId,A.UpdateTime,A.EditAdminId,A.EditAdminName,A.VehicleTransportBusinessConfigId,A.VehicleTypeName,
						  A.TransportBusiness,A.VehicleTypeId,A.Enabled,A.VehicleTypeRemark,A.MaxWithCarPeople,A.CreateAdminId,A.CreateAdminName,A.CreateTime,B.VehiclePicPath,B.VehicleLength,B.VehicleWidth,B.VehicleHeight  
						  FROM vehiclebusinessmapping A INNER JOIN VehicleType B ON A.VehicleTypeId=B.VehicleTypeId WHERE A.Enabled=1; ";
			return await this.databaseContext.Master.QueryAsync<VehicleBusinessMappingForAppEntity>(sql);
		}
		#endregion
	}
}



